package org.lex.ftplist.ui.util;

import java.awt.Component;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.TreeModel;

import org.apache.commons.net.ftp.FTPFile;
import org.lex.collection.tree.ITreeNode;
import org.lex.collection.tree.SwingTreeModel;
import org.lex.collection.tree.TreeNode;
import org.lex.ftplist.ui.common.FtpFileRowModel;
import org.netbeans.swing.outline.DefaultOutlineCellRenderer;
import org.netbeans.swing.outline.DefaultOutlineModel;
import org.netbeans.swing.outline.Outline;
import org.netbeans.swing.outline.OutlineModel;
import org.netbeans.swing.outline.RowModel;

public class FtpTreeUtils {

	public static Outline createFtpListTree(ITreeNode root) {
		TreeModel treeModel = new SwingTreeModel(root);
		RowModel rowModel = new FtpFileRowModel();
		OutlineModel model = DefaultOutlineModel.createOutlineModel(treeModel, rowModel, false, "File");
		Outline outline = new Outline(model);
		outline.setRootVisible(false);
		final TableColumnModel columnModel = outline.getColumnModel();
		// size renderer
		columnModel.getColumn(1).setCellRenderer(new DefaultTableCellRenderer() {

			@Override
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
					boolean hasFocus, int row, int column) {
				super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				String sizeStr = "";
				Object data = ((TreeNode) table.getValueAt(row, 0)).getData();
				if (((FTPFile) data).isFile()) {
					sizeStr = (((Long) value) / 1000) + " KB";
				}
				this.setText(sizeStr);
				this.setHorizontalAlignment(JLabel.TRAILING);
				return this;
			}
		});
		// date render
		columnModel.getColumn(2).setCellRenderer(new DefaultTableCellRenderer() {

			DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);

			@Override
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
					boolean hasFocus, int row, int column) {
				super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				String text = format.format((Date) value);
				this.setText(text);
				return this;
			}
		});
		// FTPFile render
		outline.setDefaultRenderer(Object.class, new DefaultOutlineCellRenderer() {

			TableCellRenderer size = columnModel.getColumn(1).getCellRenderer();
			TableCellRenderer date = columnModel.getColumn(2).getCellRenderer();

			@Override
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
					boolean hasFocus, int row, int column) {
				if (1 == column) {
					return size.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				} else if (2 == column) {
					return date.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				} else {
					super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
					FTPFile file = (FTPFile) ((TreeNode) value).getData();
					this.setText(file.getName());
					return this;
				}
			}
		});
		return outline;
	}

	public static Outline createDiffTree(ITreeNode root) {
		TreeModel treeModel = new SwingTreeModel(root);
		RowModel rowModel = new RowModel() {
			@Override
			public Class<?> getColumnClass(int column) {
				return Object.class;
			}

			@Override
			public int getColumnCount() {
				return 0;
			}

			@Override
			public String getColumnName(int column) {
				return "";
			}

			@Override
			public Object getValueFor(Object value, int column) {
				return null;
			}

			@Override
			public boolean isCellEditable(Object value, int column) {
				return false;
			}

			@Override
			public void setValueFor(Object value, int column, Object newValue) {
			}
		};
		OutlineModel model = DefaultOutlineModel.createOutlineModel(treeModel, rowModel, false, "File");
		Outline outline = new Outline(model);
		outline.setRootVisible(false);
		// FTPFile render
		outline.setDefaultRenderer(Object.class, new DefaultOutlineCellRenderer() {
			@Override
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
					boolean hasFocus, int row, int column) {
				if (0 == column) {
					super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
					if (value instanceof FTPFile) {
						String name = ((FTPFile) value).getName();
						this.setText(name);
					}
					return this;
				} else {
					return table.getColumnModel().getColumn(column).getCellRenderer().getTableCellRendererComponent(
							table, value, isSelected, hasFocus, row, column);
				}
			}
		});
		return outline;
	}
}
